package com.deco.back.tags;

import freemarker.core.Environment;
import freemarker.ext.beans.BeansWrapper;
import freemarker.ext.beans.BeansWrapperBuilder;
import freemarker.template.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

/**
 * <p>
 * 功能概述:
 * </p>
 * <p>
 * 功能详述:
 * </p>
 */
@Component
public class QuickNavDirective implements TemplateDirectiveModel {

    /*========================================================================*
     *                         Public Fields (公共属性)
     *========================================================================*/

    /*========================================================================*
     *                         Private Fields (私有属性)
     *========================================================================*/

    /*========================================================================*
     *                         Construct Methods (构造方法)
     *========================================================================*/

    /*========================================================================*
     *                         Public Methods (公有方法)
     *========================================================================*/

    @Override
    public void execute(Environment environment, Map map, TemplateModel[] templateModels, TemplateDirectiveBody templateDirectiveBody) throws TemplateException, IOException {
        Object name = map.get("current");
        String current = StringUtils.EMPTY;
        if(name != null){
            current = name.toString();
        }
        environment.getOut().write(genHtml(current));
        if(templateDirectiveBody != null){
            templateDirectiveBody.render(environment.getOut());
        }
    }

    public static BeansWrapper getBeansWrapper(){
        BeansWrapper beansWrapper =
                new BeansWrapperBuilder(Configuration.VERSION_2_3_27).build();
        return beansWrapper;
    }

    private String genHtml(String name){
        String html =
                " <div class=\"row wrapper border-bottom white-bg page-heading\">" +
                " 	<div class=\"col-lg-12\">" +
                " 		<h2>" + name + "</h2>" +
                " 		<ol class=\"breadcrumb\">" +
                " 			<li class=\"breadcrumb-item\">" +
                " 				<a href=\"/admin/index.do\">首页</a>" +
                " 			</li>";
        if(StringUtils.isNotEmpty(name)){
            html +=
                " 			<li class=\"breadcrumb-item active\">" +
                " 				<strong>" + name + "</strong>" +
                " 			</li>";
        }
        html +=
                " 		</ol>" +
                " 	</div>" +
                " </div>";
        return html;
    }

    /*========================================================================*
     *                         Private Methods (私有方法)
     *========================================================================*/
}
